package com.iwaster.servlet;

import com.iwaster.utils.UploadUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

/*
 *${PACKAGE_NAME}
 *@author Ouka
 *16:33
 */
@WebServlet(name = "uploadServlet",urlPatterns ="/upload")
public class uploadServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        //保存上传文件的目录
        String path = this.getServletContext().getRealPath("/WEB-INF/upload");
        File dir = new File(path);
        //判断目录是否存在
        if(!dir.exists()){
            dir.mkdirs();
        }

        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        boolean b = ServletFileUpload.isMultipartContent(request);
        if(!b){
            System.out.println("普通表单");
            return;
        }
        upload.setHeaderEncoding("utf-8");
        //控制上传文件的大小
        //单个文件大小2m
        upload.setFileSizeMax(1024*1024*2);
        //总的文件大小10m
        upload.setSizeMax(1024*1024*10);


        try {


            List<FileItem> fileItems = upload.parseRequest(request);


            for (FileItem fileItem : fileItems) {
                if(fileItem.isFormField()){
                    System.out.println("普通字段");
                    String name = fileItem.getName();
                    String string = fileItem.getString("utf-8");
                    response.getWriter().println(string);
                }else{
                    String filename = fileItem.getName();
                    String ext = filename.substring(filename.lastIndexOf(".") +1);
                    System.out.println(ext);
                    List<String> exts = Arrays.asList("jpg","JPG", "png","PNG", "bmp","BMP", "gif","GIF");
                    if(!exts.contains(ext)){
                        response.getWriter().println("不允许上传的文件类型");
                        continue;
                    }

                    //创建新文件名
                    String newFileName = UploadUtils.createNewFileName(filename);
                    //创建新的存储路径
                    String newPath = UploadUtils.createNewPath(path, filename);

                    InputStream inputStream = fileItem.getInputStream();
                    FileOutputStream fos = new FileOutputStream(new File(newPath, newFileName));
                    byte[] buf =new byte[1024*4];
                    int len=0;
                    while((len=inputStream.read(buf))!=-1){
                        fos.write(buf,0,len);
                    }
                    fos.close();
                    inputStream.close();
                    System.out.println("处理一个文件");
                    response.getWriter().println(newFileName+"添加成功"+"</br>");
                    response.getWriter().println("位置"+newPath);

                }

            }

        } catch (FileUploadException e) {


        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
